<?php  
class ControllerModuleCategory extends Controller {
	protected $cat_id = 0;
	protected $paths = array();
	
	protected function index() {
		$this->language->load('module/category');
		
    	$this->data['heading_title'] = $this->language->get('heading_title');
		
		$this->load->model('catalog/cat');
		$this->load->model('tool/seo_url');
		
//		if (isset($this->request->get['paths'])) {
//			$this->paths = explode('_', $this->request->get['paths']);
//			
//			$this->cat_id = end($this->paths);
//		}
		
		$this->data['category'] = $this->getCategories(0);
												
		$this->id = 'category';

		if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/module/category.tpl')) {
			$this->template = $this->config->get('config_template') . '/template/module/category.tpl';
		} else {
			$this->template = 'default/template/module/category.tpl';
		}
		
		$this->render();
  	}
	
	protected function getCategories($parent_id, $current_paths = '') {
		$cat_id = array_shift($this->paths);
		
		$output = '';
		
		$results = $this->model_catalog_cat->getCats($parent_id);
		
		if ($results) { 
			if($parent_id!=0){
				$output .= '<ul class="listTab">';
			}
    	}
		
		foreach ($results as $result) {	
			if (!$current_paths) {
				$new_paths = $result['cat_id'];
			} else {
				$new_paths = $current_paths . '_' . $result['cat_id'];
			}
			
			$output .= '<li>';
			
			$children = '';
			
			if ($cat_id != $result['cat_id']) {
				$children = $this->getCategories($result['cat_id'], $new_paths);
			}
			
			if ($this->cat_id == $result['cat_id']) {
				$output .= '<a href="' . $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=product/cat&amp;paths=' . $new_paths)  . '">' . $result['name'] . '<br /><span class="byline">'.$result['meta_description'].'</span></a>';
			} else {
				$output .= '<a href="' . $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=product/cat&amp;paths=' . $new_paths)  . '">' . $result['name'] . '</a>';
			}
			
        	$output .= $children;
        
        	$output .= '</li>'; 
		}
 
		if ($results) {
			if($parent_id!=0){
				$output .= '</ul>';		
			}
		}	
		
		return $output;
	}		
}
?>